﻿using AntdUI;
using Super.Admin.Utils.EventAggregator.EventType;
using Super.Admin.Utils.EventAggregator;
using Super.Admin.Utils.ThemeManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.ComponentModel.Design.ObjectSelectorEditor;

namespace Super.Admin.Views.Main
{
    public partial class UcMainDrawerSettingLayoutSetting : UserControl
    {
        private bool _isLoad = false; 
        public UcMainDrawerSettingLayoutSetting()
        {
            InitializeComponent();
            this.Load += UcMainDrawerSettingLayoutSetting_Load;
        }

        private void UcMainDrawerSettingLayoutSetting_Load(object? sender, EventArgs e)
        {
            _isLoad = true;
            InitializeControls();
            // 初始化本地参数
            InitializeLoadConfig();
            _isLoad = false;
        }

        private void InitializeControls()
        {
            CmbTabMoreButtonStyle.Items.AddRange(new object[] { "不显示", "更多按钮", "左右按钮", "左右按钮+阴影" });
            CmbLabelStyle.Items.AddRange(new object[] { "线条", "卡片", "轻快" });
            CmbPreferredLocationSettings.Items.AddRange(new object[] { "自动", "顶栏", "固定" });
        }
        private void InitializeLoadConfig()
        {
            // 判断侧边栏布局
            BtnSwitchMenuLayout.SelectIndex = (int)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Layout.NavigationPosition;
            // 判断是否显示侧边栏
            BtnSwitchShowSidebar.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Sidebar.DisplaySidebar;
            // 判断是否折叠菜单
            BtnSwitchCollapseMenu.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Sidebar.CollapseMenu;
            // 加载菜单宽度
            TxtMenuBarWidth.Value = (decimal)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Sidebar.MenuBarWidth;
            // 是否开启手风琴菜单
            BtnSwitchAccordionMenu.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Sidebar.AccordionMenu;
            // 是否显示顶栏
            BtnSwitchDisplayTopBar.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.TopBar.DisplayTopBar;
            // 是否显示面包屑导航
            BtnSwitchEnableBreadcrumbNavigation.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Breadcrumb.EnableBreadcrumbNavigation;
            // 是否显示面包屑图标
            BtnSwitchDisplayBreadcrumbIcon.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Breadcrumb.DisplayBreadcrumbIcon;
            // 是否显示首页按钮
            BtnSwitchDisplayHomepageButton.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Breadcrumb.DisplayHomepageButton;
            // 是否显示Tab头
            BtnSwitchEnableTabBar.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Tabs.EnableTabBar;
            // 最多标签数
            TxtMaxTabs.Value = (decimal)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Tabs.MaximumNumberOfTagPages;
            // 是否启用拖拽排序
            BtnSwitchStartDragAndDropSorting.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Tabs.StartDragAndDropSorting;
            // Tab 超出菜单样式
            CmbTabMoreButtonStyle.SelectedValue = CmbTabMoreButtonStyle.Items[(int)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Tabs.TabMoreButtonStyle];
            // Tab 样式
            CmbLabelStyle.SelectedValue = CmbLabelStyle.Items[(int)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.Tabs.LabelStyle];
            // 启用全局搜索
            BtnSwitchEnableGlobalSearch.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableGlobalSearch;
            // 启用主题切换
            BtnSwitchEnableThemeSwitch.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableThemeSwitch;
            // 启用语言切换
            BtnSwitchEnableLanguageSwitching.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableLanguageSwitching;
            // 启用通知
            BtnSwitchEnableNotification.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableNotification;
            // 启用锁屏
            BtnSwitchEnableLockScreen.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableLockScreen;
            // 启用侧边栏切换
            BtnSwitchEnableSidebarSwitch.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.EnableSidebarSwitch;
            // 启用刷新
            BtnSwitchStartRefreshing.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.StartRefreshing;
            // 偏好位置设置
            CmbPreferredLocationSettings.SelectedValue = CmbPreferredLocationSettings.Items[(int)ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.SmallComponents.PreferredLocationSettings];
            // 启用底栏
            BtnSwitchEnableTheBottomBar.Checked = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.BottomBar.EnableTheBottomBar;
            // 底栏内容
            TxtBottomBarContent.Text = ThemeSettingHelper.Instance.ThemeConfig.LayoutSettings.BottomBar.BottomBarContent;
        }

        private void BtnSwitch_SelectIndexChanged(object sender, AntdUI.IntEventArgs e)
        {
            if (_isLoad) return;
            Segmented segmented = (Segmented)sender;
            switch (segmented.Name)
            {
                case "BtnSwitchMenuLayout":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.NavigationPosition, Value = segmented.SelectIndex });
                    break;
            }
        }

        private void BtnSwitch_CheckedChanged(object sender, BoolEventArgs e)
        {
            if (_isLoad) return;
            Switch @switch = (Switch)sender;
            switch (@switch.Name)
            {
                case "BtnSwitchShowSidebar":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.ShowSidebar, Value = e.Value });
                    break;
                case "BtnSwitchCollapseMenu":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.CollapseMenu, Value = e.Value });
                    break;
                case "BtnSwitchDisplayTopBar":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.DisplayTopBar, Value = e.Value });
                    break;
                case "BtnSwitchAccordionMenu":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.AccordionMenu, Value = e.Value });
                    break;
                case "BtnSwitchEnableBreadcrumbNavigation":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableBreadcrumbNavigation, Value = e.Value });
                    break;
                case "BtnSwitchDisplayBreadcrumbIcon":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.DisplayBreadcrumbIcon, Value = e.Value });
                    break;
                case "BtnSwitchDisplayHomepageButton":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.DisplayHomepageButton, Value = e.Value });
                    break;
                case "BtnSwitchEnableTabBar":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableTabBar, Value = e.Value });
                    break;
                case "BtnSwitchStartDragAndDropSorting":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.StartDragAndDropSorting, Value = e.Value });
                    break;
                case "BtnSwitchEnableGlobalSearch":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableGlobalSearch, Value = e.Value });
                    break;
                case "BtnSwitchEnableThemeSwitch":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableThemeSwitch, Value = e.Value });
                    break;
                case "BtnSwitchEnableLanguageSwitching":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableLanguageSwitching, Value = e.Value });
                    break;
                case "BtnSwitchEnableLockScreen":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableLockScreen, Value = e.Value });
                    break;
                case "BtnSwitchEnableSidebarSwitch":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableSidebarSwitch, Value = e.Value });
                    break;
                case "BtnSwitchStartRefreshing":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.StartRefreshing, Value = e.Value });
                    break;
                case "BtnSwitchEnableNotification":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableNotification, Value = e.Value });
                    break;
                case "BtnSwitchEnableTheBottomBar":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.EnableTheBottomBar, Value = e.Value });
                    break;
            }
        }

        private void TxtMenuBarWidth_ValueChanged(object sender, DecimalEventArgs e)
        {
            if (_isLoad) return;
            EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.MenuBarWidth, Value = (int)e.Value });
        }

        private void TxtNumber_MouseEnter(object sender, EventArgs e)
        {
            if (_isLoad) return;
            InputNumber inputNumber = (InputNumber)sender;
            inputNumber.WheelModifyEnabled = true;
        }

        private void TxtNumber_MouseLeave(object sender, EventArgs e)
        {
            if (_isLoad) return;
            InputNumber inputNumber = (InputNumber)sender;
            inputNumber.WheelModifyEnabled = false;
        }

        private void TxtMaxTabs_ValueChanged(object sender, DecimalEventArgs e)
        {
            if (_isLoad) return;
            EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.MaximumNumberOfTagPages, Value = (int)e.Value });
        }

        private void ComboBox_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            if (_isLoad) return;
            Select select = (Select)sender;
            switch (select.Name)
            {
                case "CmbTabMoreButtonStyle":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.TabMoreButtonStyle, Value = CmbTabMoreButtonStyle.Items.IndexOf(e.Value) });
                    break;
                case "CmbLabelStyle":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.LabelStyle, Value = CmbLabelStyle.Items.IndexOf(e.Value) });
                    break;
                case "CmbPreferredLocationSettings":
                    EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.PreferredLocationSettings, Value = CmbPreferredLocationSettings.Items.IndexOf(e.Value) });
                    break;
            }
        }

        private void TxtBottomBarContent_TextChanged(object sender, EventArgs e)
        {
            if (_isLoad) return;
            EventAggregator.Default.Publish<LayoutSettingEvent>(new LayoutSettingEvent { Type = LayoutSettingEvent.LayoutSettingType.BottomBarContent, Value = TxtBottomBarContent.Text });
        }
    }
}
